from flask import (
    Blueprint,
    request, render_template
)

from flask import current_app as app

import json

user_ctrl = Blueprint('user', __name__, url_prefix='/user')


@user_ctrl.route('/login', methods=['POST'])
def login():
    """
    user login processor
    :return:
    """
    return 'login'


@user_ctrl.route('/register', methods=['POST'])
def register():
    return render_template("auth/register.html")


def check_body():
    """
    检查传入参数是否为json格式
    :return:
    """
    content_type = request.headers.get('Content-Type')
    app.logger.info(f"Content-Type: {content_type}")
    if content_type == 'application/json':
        req_body = request.json
    else:
        try:
            req_body = json.loads(request.data)
        except Exception as e:
            app.logger.error(f"unknown data type received: {request.data}")
            raise e
    app.logger.info(f"request body: {req_body}")

    return req_body
